import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI, { Message } from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import { generateUUID } from './utils/uuid/uuid'
import { checkMenuById } from './utils/menu/menu'
import VueClipboard from 'vue-clipboard2'
import dialogDrag from '@/utils/drag/drag'
import drag from '@/utils/drag/drag2'
import { homePosition } from '@/utils/constant'
import VueCompositionAPI from '@vue/composition-api'
import Svg from '@/components/svgIcon/index'
import oldBus from '@/utils/oldBus'
import hasPermission from './directives'
import { filterGlobalPermission } from '@/directives'
import { FLY_DURATION } from '@/api/config'
Vue.component('svgIcon', Svg)
hasPermission(Vue)

/**
 * require.context 的参数说明
 * './svg' 代表要查找的文件路径
 * false 代表是否查找子目录
 * /\.svg$/ 代表要匹配文件的正则
 *
 */
const req = require.context('./icons/svg', false, /\.svg$/)
// console.log('req->', req.keys())
const requireAll = (requireContext) =>
  requireContext.keys().map(requireContext)
requireAll(req)
Vue.use(VueCompositionAPI)

Vue.config.productionTip = false
Vue.prototype.$message = Message
Vue.prototype.$bus = oldBus
Vue.prototype.generateUUID = generateUUID
Vue.prototype.checkMenuById = checkMenuById
Vue.prototype.$filterGlobalPermission =
  filterGlobalPermission
Vue.use(ElementUI)
Vue.use(dialogDrag)
Vue.use(drag)

Vue.use(VueClipboard)

if (FLY_DURATION) {
  homePosition.duration = FLY_DURATION
}

export const app = new Vue({
  router,
  store,
  render: (h) => h(App)
}).$mount('#app')

// 监听 chunk 加载失败，自动刷新
window.addEventListener(
  'error',
  function (e) {
    const target = e.target || e.srcElement
    if (
      target.tagName === 'SCRIPT' &&
      target.src.includes('chunk')
    ) {
      console.warn('Chunk 加载失败，自动刷新中...')
      window.location.reload()
    }
  },
  true
)
